package finbudgettable.client.ui.grid.cell.footer;

import java.math.BigDecimal;

import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.text.shared.SafeHtmlRenderer;

import finbudget.shared.Constants;

public class BigDecimalRenderer implements SafeHtmlRenderer<BigDecimal> {

	private static BigDecimalRenderer instance;
	private final static NumberFormat format = NumberFormat.getFormat(Constants.formatCurrency);

	public static BigDecimalRenderer getInstance() {
		if (instance == null) {
			instance = new BigDecimalRenderer();
		}
		return instance;
	}

	private BigDecimalRenderer() {
	}

	@Override
	public SafeHtml render(BigDecimal object) {
		return (object == null) ? SafeHtmlUtils.EMPTY_SAFE_HTML : SafeHtmlUtils.fromString(format.format(object));
	}

	@Override
	public void render(BigDecimal object, SafeHtmlBuilder builder) {
		builder.append(render(object));
	}

}
